
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;


public class Server {
	public static String message = null;
	public static String try_message = null;
   DatagramSocket theSocket = null;
   String ultimo = "diferente";
   public Server () {      
      
      try {
         theSocket = new DatagramSocket(1232);
         //System.out.println("Server created and waiting for incomming connections...");
      } catch (SocketException e) {
         // TODO Auto-generated catch block
         System.out.println("Error creating the socket: " + e.getMessage());
      }
   }
   
   public void receber() {
      DatagramPacket theRecievedPacket;
      byte[] inBuffer;
        // create some space for the text to send and recieve data
        inBuffer = new byte[500];
        // create a place for the client to send data too
        theRecievedPacket = new DatagramPacket(inBuffer, inBuffer.length);
        // wait for a client to request a connection
        try {
	        theSocket.receive(theRecievedPacket);
	        if (!ultimo.equals(new String(theRecievedPacket.getData(), 0,theRecievedPacket.getLength()).split("@")[1])){
	    	message = new String(theRecievedPacket.getData(), 0,theRecievedPacket.getLength());
	    	Client.enviar(message);
	    	ultimo = message.split("@")[1];
	        }
	        else message =null;
        } catch (IOException e) {
        	System.out.println("Error receiving " + e.getMessage());
		}
      
      // close the server socket
   }
}
